package com.epfresh.supplier.activity.standingbook;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.epfresh.api.constant.AppSupplyRouterConstant;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.widget.list.LoadMoreListView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.CancelDebtRecordAdapter;
import com.epfresh.supplier.entity.CancelDebtRecordEntity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by shenjie on 2017/9/22.
 */

public class CancelDebtRecordActivity extends BaseActivity implements LoadMoreListView.IXListViewListener{
    private LoadMoreListView resultList;
    private ArrayList<CancelDebtRecordEntity.CancelDebtList> debtLists = new ArrayList<>();
    private LinearLayout llImage;
    private CancelDebtRecordAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        loadHttpRequest();
    }

    private void initView(){
        toolbarTitle.setText("收款记录");
//        toolbarTitle.setTextColor(getResources().getColor(R.color.font_color_def));
//        float textsize = getResources().getDimension(R.dimen.text_size_max_18);
//        final float scale = getResources().getDisplayMetrics().density;
//        float size = (textsize / scale + 0.5f);
//        toolbarTitle.setTextSize(size);
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        setContentView(R.layout.activity_canceldebt_record);
        resultList= (LoadMoreListView) findViewById(R.id.canceldebt_list);
        llImage= (LinearLayout) findViewById(R.id.imagecon);
        adapter=new CancelDebtRecordAdapter(this,debtLists);
        resultList.setAdapter(adapter);
        resultList.setXListViewListener(this);
    }

    int currentNum=-1;
    private void loadHttpRequest() {

//        "storeId":"586",
//                "pageNumber":0,
//                "pageSize":10,
//                "buyer":"",
//                "buyerId":1
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(AppSupplyRouterConstant.cmd_finance_payment_pos_findPayInfoPage);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        Map<String, String> map = new HashMap<>();
        map.put("storeId", getAppApplication().getStoreId());
        currentNum++;
        map.put("pageNumber", currentNum+"");
        map.put("pageSize",5+"");
//        map.put("buyer","");
//        map.put("buyerId","");
        data.setParameters(map);
        request(data, AppSupplyRouterConstant.cmd_finance_payment_pos_findPayInfoPage, onRequestListener);
    }

    private OnRequestListener<CancelDebtRecordEntity> onRequestListener = new OnRequestListener<CancelDebtRecordEntity>() {
        @Override
        public CancelDebtRecordEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr,CancelDebtRecordEntity.class);

        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<CancelDebtRecordEntity> response, Object tag) {
            hideProgressDialog();
            CancelDebtRecordEntity entity=response.getResponseElement();
            if(entity!=null){
                currentNum=entity.getNumber();
                if(entity.isLast()){
                    resultList.noMore();
                }else {
                    resultList.setPullLoadEnable(true);
                }
                debtLists.addAll(entity.getContent());
            }
            if(debtLists.size()>0){
                llImage.setVisibility(View.INVISIBLE);
                resultList.setVisibility(View.VISIBLE);
            }else {
                llImage.setVisibility(View.VISIBLE);
                resultList.setVisibility(View.INVISIBLE);
            }
            adapter.notifyDataSetChanged();
        }


    };

    @Override
    public void onLoadMore() {
        loadHttpRequest();
    }
}
